package com.netbao.controller;

import com.netbao.excetion.RRException;
import com.netbao.model.Result;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;


/**
 * @author songc
 * @create 2019-08-29 10:21
 */
@ControllerAdvice
public class MyException {
    @ResponseBody
    @ExceptionHandler(RRException.class)
    public Object RRException(RRException e) {
        return Result.error(e.getMessage());
    }

//    @ResponseBody
//    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
//    public Object SQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
//        return Result.error(e.getMessage());
//    }

    /*@ExceptionHandler(Exception.class)
    public String err(Exception e){
            return "forward:/error";
    }*/
    @ResponseBody
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Object duplicate(SQLIntegrityConstraintViolationException e) {
        return Result.error("重复记录");
    }

    @ResponseBody
    @ExceptionHandler(DuplicateKeyException.class)
    public Object duplicate(DuplicateKeyException e) {
        return Result.error("重复记录" + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Object runtimeexception(RuntimeException e) {
        e.printStackTrace();
        return Result.error("系统错误，请与管理员联系");
    }


    @ResponseBody
    @ExceptionHandler(UnauthorizedException.class)
    public Object UnauthorizedException(UnauthorizedException e) {
        e.printStackTrace();
        return Result.error("没有操作权限，不能进行此项操作");
    }

}
